package com.xiaohai.wx.weather.dao;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import com.spaceprogram.simplejpa.EntityManagerFactoryImpl;
import com.xiaohai.wx.weather.dto.User;
import com.xiaohai.wx.weather.util.AwsStageUtils;

@Repository("UserDao")
public class UserDao{

	private static EntityManagerFactoryImpl factory;
	
	public UserDao(){
		String currentStage = AwsStageUtils.getResourceSuffixForCurrentStage();
		factory = new EntityManagerFactoryImpl("WeixinWeather"+currentStage , null);
	}
    
    public void deleteUser(User user)  {
        this.deleteUserById(user.getFakeId());
    }
    
    public void deleteUserById(String openId){
    	EntityManager em = null;
        User user = this.queryUserByOpenId(openId);
        
        try {
            em = factory.createEntityManager();
            em.remove(user);
        }
        finally {
            if (em!=null) {
                em.close();
            }
        }    	
    }

    public void addOrReplaceUser(User user) {
  
        EntityManager em = null;
        try {
            em = factory.createEntityManager();
            em.persist(user);
        }
        finally {
            if (em!=null) {
                em.close();
            }
        }
    }
    
    public void deleteAllUsers(){
    	List<User> userList = this.queryAllUser();
    	EntityManager em = null;
    	
    	try {
            em = factory.createEntityManager();
            for(User user : userList){
            	 em.remove(user);
        	}
        }
        finally {
            if (em!=null) {
                em.close();
            }
        }
    }

    @SuppressWarnings("unchecked")
    public List<User> queryAllUser() {
    	EntityManager em = null;
        try {
            em = factory.createEntityManager();
            Query query = em.createQuery("select u from com.xiaohai.wx.weather.dto.User u");
            
            List<User> userList = query.getResultList();
            return userList;
        }
        finally {
            if (em!=null) {
                em.close();
            }
        }
    }

    public User queryUserByOpenId(String openId) {
        EntityManager em = null;
        try {
            em = factory.createEntityManager();
            User user = em.find(User.class, openId);
            return user;
        }
        finally {
            if (em!=null) {
                em.close();
            }
        }
    }
	
    /*
	public WeixinUser queryUserByFakeId(String fakeId) {
		EntityManager em = null;
        try {
            em = factory.createEntityManager();
            Query query = em.createQuery("select u from User where u.fakeId= :fId");
            query.setParameter("fId", fakeId);
            return (User) query.getSingleResult();
        }
        finally {
            if (em!=null) {
                em.close();
            }
        }
	}*/
	
	@SuppressWarnings("unused")
	private void testCreate(){
		User user = new User();
		user.setOpenId("10");
		user.setLocation("Beijing22");
		user.setNickName("Jason22");
		user.setGuanzhuTime(new Date());
		this.addOrReplaceUser(user);
	}
	
	
	@SuppressWarnings("unused")
	private void testDelete(){
		User user = new User();
		user.setOpenId("10");
		user.setLocation("Beijing");
		user.setNickName("Jason");
		this.deleteUser(user);
	}

	private void testQueryAll(){
		List<User> userList = this.queryAllUser();
		for(User user: userList){
			System.out.println(user.toString());
		}
	}
	
	@SuppressWarnings("unused")
	private void testQueryByOpenId(){
		User user = this.queryUserByOpenId("10");
		System.out.println(user.toString());
	}
	
	public static void main(String[] args){
		
		UserDao userDao = new UserDao();
		
		
		//userDao.deleteAllUsers();

		//userDao.testCreate();
		/*
		User user = userDao.queryUserByOpenId("10");
		user.setFakeId("");
		user.setMapKey("山东,河南,河北");
		user.setLocation("北京,淮南,朔州");
		userDao.addOrReplaceUser(user);
		*/
		userDao.testQueryAll();
	
	}

}
